package ${packageCode}.${moduleMapperCode}.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import ${packageCode}.${moduleMapperCode}.dto.${businessCode}Dto;
import ${packageCode}.${moduleMapperCode}.entity.${businessCode};
import ${packageCode}.${moduleMapperCode}.mapper.${businessCode}Mapper;
import ${packageCode}.${moduleMapperCode}.service.${businessCode}Service;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.transaction.annotation.Transactional;
import top.cardone.context.ApplicationContextHolder;
import top.cardone.context.annotation.Event;
import top.cardone.context.annotation.Events;
import top.cardone.context.event.SimpleErrorEvent;
import top.cardone.context.event.SimpleEvent;
import top.cardone.context.util.MapUtils;
import top.cardone.context.util.StringUtils;
import top.cardone.data.mybatisplus.service.impl.MybatisPlusServiceImpl;
import top.cardone.data.support.PageSupport;
import top.cardone.data.support.PageableSupport;

import java.io.Serializable;
import java.util.List;
import java.util.Map;

/**
 * ${moduleName}-${businessName}服务
 *
 * @author yao hai tao
 */
@DubboService(protocol = "dubbo")
@Events({@Event(applicationEvent = SimpleErrorEvent.class),
        @Event(applicationEvent = SimpleEvent.class)})
public class ${businessCode}ServiceImpl extends MybatisPlusServiceImpl<${businessCode}Mapper, ${businessCode}> implements ${businessCode}Service {
    @Override
    @Transactional
    public boolean updateById(${businessCode}Dto updateParam) {
        boolean update = this.updateById((${businessCode}) updateParam);

        return update;
    }

    @Override
    @Transactional
    public boolean save(${businessCode}Dto saveParam) {
        boolean save = this.save((${businessCode}) saveParam);

        return save;
    }

    @Override
    public ${businessCode} getBy${businessCode}Code(Serializable ${(businessCode?uncap_first)!''}Code) {
        LambdaQueryChainWrapper<${businessCode}> lambdaQuery = this.lambdaQuery();

//		lambdaQuery.eq(${businessCode}::get${businessCode}Code, ${(businessCode?uncap_first)!''}Code);

        return lambdaQuery.one();
    }

    @Override
    public List<${businessCode}> list(Map<String, Object> param) {
        LambdaQueryChainWrapper<${businessCode}> lambdaQuery = this.lambdaQuery();

        String term = MapUtils.getString(param, "term");

        if (StringUtils.isNotBlank(term)) {
//            lambdaQuery
//                    .like(${businessCode}::getZhCnName, term)
//                    .or()
//                    .like(${businessCode}::getEnName, term)
//                    .or()
//                    .like(${businessCode}::get${businessCode}Code, term);
        }

//        lambdaQuery.last("limit 0 , 10");

//        lambdaQuery.orderByAsc(${businessCode}::get${businessCode}Code);

        return lambdaQuery.list();
    }

    @Override
    public <E extends Page<${businessCode}>> E page(Map<String, Object> param) {
        LambdaQueryChainWrapper<${businessCode}> lambdaQuery = this.lambdaQuery();

        String ${(businessCode?uncap_first)!''}Id = MapUtils.getString(param, "${(businessCode?uncap_first)!''}Id");

        if (StringUtils.isNotBlank(${(businessCode?uncap_first)!''}Id)) {
            lambdaQuery.eq(${businessCode}::get${businessCode}Id, ${(businessCode?uncap_first)!''}Id);
        } else {
            String ${(businessCode?uncap_first)!''}Name = MapUtils.getString(param, "${(businessCode?uncap_first)!''}Name");

            if (StringUtils.isNotBlank(${(businessCode?uncap_first)!''}Name)) {
//                lambdaQuery
//                        .like(${businessCode}::getZhCnName, ${(businessCode?uncap_first)!''}Name)
//                        .or()
//                        .like(${businessCode}::getEnName, ${(businessCode?uncap_first)!''}Name);
            }
        }

        String ${(businessCode?uncap_first)!''}Code = MapUtils.getString(param, "${(businessCode?uncap_first)!''}Code");

        if (StringUtils.isNotBlank(${(businessCode?uncap_first)!''}Code)) {
//            lambdaQuery.like(${businessCode}::get${businessCode}Code, ${(businessCode?uncap_first)!''}Code);
        }

        Pageable pageable = this.getPageable(param);

        IPage<${businessCode}> mybatisplusPage = lambdaQuery.page(this.getMybatisplusPage(pageable));

        return (E) this.newPage(mybatisplusPage, pageable);
    }

    @Override
    public Page<${businessCode}Dto> pageByParam(Map<String, Object> param) {
        Pageable pageable = this.getPageable(param);

        IPage<${businessCode}Dto> mybatisplusPage = this.baseMapper.pageByParam(this.getMybatisplusPage(pageable), param);

        return this.newPage(mybatisplusPage, pageable);
	}
}